<template>
  <div>
    <el-tree
      ref="tree"
      check-strictly
      show-checkbox
      default-expand-all
      node-key="id"
      :data="permission"
      :props="{label:'name'}"
    />
    <div class="footer">
      <el-button size="small" @click="$emit('close')">取消</el-button>
      <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
import { assignPermission, getAssignDetail } from '@/api/settings'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      permission: []
    }
  },
  created() {
    this.loadPermission()
    this.loadPermissionDetail()
  },
  methods: {
    // 获取权限
    async  loadPermission() {
      const res = await getPermissionList()
      // console.log(res)
      this.permission = tranListToTreeData(res.data)
    },
    // 获取角色权限
    async  loadPermissionDetail() {
      const res = await getAssignDetail(this.id)
      // console.log(res)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    // 点击确定
    async hSubmit() {
      try {
        const re = this.$refs.tree.getCheckedKeys()
        // console.log(re)
        const res = await assignPermission({ id: this.id, permIds: re })
        // console.log(res)
        this.$message.success(res.message)
        this.$emit('close')
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  }

}
</script>

<style scoped lang="scss">
.footer{
  display: flex;
  justify-content: end;
}
</style>
